"""
Case Type   : cast
Case Name   : cast转换select 1234::int::circle_tb1
Create At   : 2024.5.25
Owner       : @zou_jialiang050
Description :
    1、创建类型及函数
    2、创建自定义转换
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest001(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()

    def test_cast(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:创建类型及函数;expect:成功----'
        self.log.info(text)
        sql_cmd = "create type circle_tb1;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE TYPE', sql_res, '执行失败')

        sql_cmd = "create function f1(cstring) returns " \
                  "circle_tb1 strict immutable language internal as 'int8in';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE FUNCTION', sql_res, '执行失败')

        sql_cmd = "create function moneytmp(circle_tb1) returns " \
                  "cstring as 'textout' language internal strict immutable;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE FUNCTION', sql_res, '执行失败')

        sql_cmd = "create function f2(circle_tb1) returns " \
                  "cstring strict immutable language internal as 'int8out';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE FUNCTION', sql_res, '执行失败')

        sql_cmd = "create type circle_tb1(internallength = 8, " \
                  "input = f1,  output = f2, alignment = double, " \
                  "passedbyvalue = true);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE TYPE', sql_res, '执行失败')

        text = '----step2:创建转换;expect:成功----'
        self.log.info(text)
        sql_cmd = "create cast(int8 as circle_tb1) without function;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE CAST', sql_res, '执行失败')

        sql_cmd = "create cast(int4 as circle_tb1) with inout;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE CAST', sql_res, '执行失败')

        sql_cmd = "select 1234::int4::circle_tb1;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('1234', sql_res, '执行失败')

    def tearDown(self):
        self.log.info('----step3:清理环境;expect:成功----')
        sql_cmd = "drop type circle_tb1 cascade;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('DROP TYPE', sql_res, '执行失败')
        self.log.info(f'----{os.path.basename(__file__)} end-----')
